/**
 * 旭鸣应用工具包命名空间
 * 
 * @return
 */
function XMAppUtils() {
};

function breakpage(url) {
	var _parentWin = window.parent;
	var tabs = _parentWin.mini.get("menu-tabs-div");
	var tab = tabs.getActiveTab();
	
	if (tab) {
		tab.LntD.attributes.item(3).nodeValue = url;
	}
}

/**
 * Date格式化成字符串 返回时间字符串
 * @param {} value
 * @param {} t
 * @return {}
 */
function changeDate(value, t) {
	var CurrentDate = "";
	
	try {
		if (t > 0 && t < 7) {
			var date = new Date(value["time"]);
			var array = [date.getFullYear(), date.getMonth() + 1,
					date.getDate(), date.getHours(), date.getMinutes(),
					date.getSeconds()];
			CurrentDate = array[0];
			
			for (var i = 1; i < t; i++) {
				var l = i < 3 ? "-" : ":";
				if (i == 3)
					l = " ";
				if (array[i] >= 10) {
					CurrentDate += l + array[i];
				} else {
					CurrentDate += l + "0" + array[i];
				}
			}
		}
	} catch (e) {
	}
	
	return CurrentDate;
};

/**
 * 时间对象的格式化;
 */
Date.prototype.format = function(format) {
	/*
	 * eg:format="YYYY-MM-dd hh:mm:ss";
	 */
	var o = {
		"M+" : this.getMonth() + 1, // month
		"d+" : this.getDate(), // day
		"h+" : this.getHours(), // hour
		"m+" : this.getMinutes(), // minute
		"s+" : this.getSeconds(), // second
		"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
		"S" : this.getMilliseconds()
		// millisecond
	}
	
	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
						- RegExp.$1.length));
	}
	
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1
							? o[k]
							: ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
}

XMAppUtils.instanc = XMAppUtils;

/**
 * 格式化日期函数，可以直接调用
 * 
 * @param dtime
 *            日期对象
 * @param fmt
 *            格式字符串 可以为空
 * @return
 */
XMAppUtils.formatDate = function(fmt, dtime) {
	if (!fmt)
		fmt = "yyyy-MM-dd HH:mm:ss";
	if (!dtime)
		dtime = this;
		
	var o = {
		"M+" : dtime.getMonth() + 1, // 月份
		"d+" : dtime.getDate(), // 日
		"h+" : dtime.getHours() % 12 == 0 ? 12 : dtime.getHours() % 12, // 小时
		"H+" : dtime.getHours(), // 小时
		"m+" : dtime.getMinutes(), // 分
		"s+" : dtime.getSeconds(), // 秒
		"q+" : Math.floor((dtime.getMonth() + 3) / 3), // 季度
		"S" : dtime.getMilliseconds()
		// 毫秒
	};
	
	var week = {
		"0" : "\u65e5",
		"1" : "\u4e00",
		"2" : "\u4e8c",
		"3" : "\u4e09",
		"4" : "\u56db",
		"5" : "\u4e94",
		"6" : "\u516d"
	};
	
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (dtime.getFullYear() + "").substr(4
						- RegExp.$1.length));
	}
	
	if (/(E+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1)
						? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468")
						: "")
						+ week[dtime.getDay() + ""]);
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1)
							? (o[k])
							: (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	
	return fmt;
};

/**
 * 将字符串转化为日期对象
 * 
 * @param Str
 *            日期字符串
 * @return
 */
XMAppUtils.str2DateTime = function(strDate) {
	return new Date(strDate.replace(/-/g, '/'));
};

/**
 * 将如下格式的日期对象转换为 js 日期对象 格式如下：
 * {"date":15,"day":4,"hours":23,"minutes":31,"month":8,"nanos":0,"seconds":43,"time":1316100703000,"timezoneOffset":-480,"year":111}
 */
XMAppUtils.obj2DateTime = function(obj) {
	// alert(Edo.util.Json.encode(obj));
	// var c =
	// {"date":15,"day":4,"hours":23,"minutes":31,"month":8,"nanos":0,"seconds":43,"time":1316100703000,"timezoneOffset":-480,"year":111}
	if (obj != null && obj != "" && obj != "null") {
		var ddate = new Date(obj.year + 1900, obj.month, obj.date, obj.hours,
				obj.minutes, obj.seconds);
		return ddate;
	}
};

/**
 * 按指定格式将字符串转换为日期类型
 * 
 * @param strDate
 *            日期字符串
 * @param _T
 *            日期与时间的间隔符 默认为空格" "
 * @param _YMD
 *            年月分隔符 默认为短横线"-"
 * @param _HMS
 *            时间分隔符 默认为冒号 ":"
 */
XMAppUtils.str2Date = function(strDate, _T, _YMD, _HMS) {
	if (!_T)
		_T = " ";
	if (!_YMD)
		_YMD = "-";
	if (!_HMS)
		_HMS = ":";
		
	if (typeof strDate == 'string') {
		var dt = new Array();
		var d = new Array();
		var t = new Array();
		dt = strDate.split(_T);
		d = dt[0].split(_YMD);
		if (dt.length > 1) {
			t = dt[1].split(_HMS);
		}
		var newDate = new Date(d[0], d[1] - 1, d[2]);
		if (t.length > 0)
			newDate = new Date(d[0], d[1] - 1, d[2], t[0], t[1], t[2]);
		return newDate;
	} else {
		return new Date();
	}
};

/**
 * 字符串转换为日期类型
 * 
 * @param str
 *            日期字符串
 */
XMAppUtils.str2date = function(str) {
	if (typeof str == 'string') {
		var results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) *$/);
		if (results && results.length > 3)
			return new Date(parseInt(results[1]), parseInt(results[2]) - 1,
					parseInt(results[3]));
		results = str
				.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(0|00|\d{1,2}):(\d{1,2}):(\d{1,2}) *$/);
		if (results && results.length > 6)
			return new Date(parseInt(results[1]), parseInt(results[2]) - 1,
					parseInt(results[3]), parseInt(results[4]),
					parseInt(results[5]), parseInt(results[6]));
		results = str
				.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{1,9}) *$/);
		if (results && results.length > 7)
			return new Date(parseInt(results[1]), parseInt(results[2]) - 1,
					parseInt(results[3]), parseInt(results[4]),
					parseInt(results[5]), parseInt(results[6]),
					parseInt(results[7]));
	}
	return new Date();
};

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};

/**
 * 可以直接在日期对象上调用 formatDate 函数
 */
Date.prototype.formatDate = XMAppUtils.formatDate;

/**
 * 将字符串转化为日期对象
 * 
 * @param Str
 *            日期字符串
 * @return
 */
XMAppUtils.parseStr = function(Str) {
	return XMAppUtils.strToDateTime(Str);
};

/**
 * 自动将图片按比列缩放 使用方法如下： <img src='xxxxxx' onload='setImgSize(90,60,this)'/>
 * 系统将按设定的宽，高自动按图片比例匹配显示
 * 
 * @param width
 *            标准宽度
 * @param height
 *            标准高度
 * @param imgObj
 *            图片对象
 * @return
 */
XMAppUtils.setImgSize = function(width, height, imgObj) {
	if (!width)
		width = 72;
	if (!height)
		height = 72;
	myImage = new Image();
	myImage.src = imgObj.src;
	if (myImage.width > 0 && myImage.height > 0) {
		var rate = 1;
		if (myImage.width > width || myImage.height > height) {
			if (width / myImage.width < height / myImage.height) {
				rate = width / myImage.width;
			} else {
				rate = height / myImage.height;
			}
		}
		if (window.navigator.appName == "Microsoft Internet Explorer") {
			imgObj.style.zoom = rate;
		} else {
			imgObj.width = myImage.width * rate;
			imgObj.height = myImage.height * rate;
		}
	}
};

/**
 * 通过对Array的扩展，实现了对javascript Array对象通过索引移除数组中的一个元素。
 * 
 * eg: a = ['1','2','3','4','5']; var dx = a.getIndexByValue("2"); a.remove(dx);
 * 
 * @param {}
 *            dx 被删除元素的下标
 * @return 删除元素之后返回新数组
 */
/*
 * Array.prototype.remove = function(dx) { if (isNaN(dx) || dx > this.length) {
 * return false; } for (var i = 0, n = 0; i < this.length; i++) { if (this[i] !=
 * this[dx]) { this[n++] = this[i] } } this.length -= 1 }
 */

/**
 * 在数组中获取指定值的元素索引
 * 
 * @param {}
 *            value
 * @return {}
 */
/*
 * Array.prototype.getArrayIndexByValue = function(value) { var index = -1; for
 * (var i = 0; i < this.length; i++) { if (this[i] == value) { index = i; break; } }
 * return index; }
 */

/**
 * 将日期对象转变为string
 * 
 * @param {}
 *            dateObj
 * @return 返回（日期）格式字符串，如2012-06-16
 */
XMAppUtils.changeDateToDateStr = function(dateObj) {
	var Year = 0;
	var Month = 0;
	var Day = 0;
	var CurrentDate = "";

	var date = new Date(dateObj["time"]);
	Year = date.getFullYear();
	Month = date.getMonth() + 1;
	Day = date.getDate();
	CurrentDate = Year + "-";

	if (Month >= 10) {
		CurrentDate = CurrentDate + Month + "-";
	} else {
		CurrentDate = CurrentDate + "0" + Month + "-";
	}

	if (Day >= 10) {
		CurrentDate = CurrentDate + Day;
	} else {
		CurrentDate = CurrentDate + "0" + Day;
	}

	return CurrentDate;
};

/**
 * 将日期对象转变为string
 * 
 * @param {}
 *            dateObj 日期对象
 * @param {}
 *            showSec 是否精确到秒数，true || null：精确到秒数；false：不精确到秒数
 * @return 根据showSec返回（日期+时间）格式字符串，如2012-06-16 16:36 || 2012-06-16 16:36:36
 */
XMAppUtils.changeDateToDateTimeStr = function(dateObj, showSec) {
	var Year = 0;
	var Month = 0;
	var Day = 0;
	var Hour = 0;
	var Min = 0;
	var Sec = 0;
	var CurrentDate = "";
	var CurrentTime = "";

	var date = new Date(dateObj["time"]);
	Year = date.getFullYear();
	Month = date.getMonth() + 1;
	Day = date.getDate();
	Hour = date.getHours();
	Min = date.getMinutes();
	Sec = date.getSeconds();
	CurrentDate = Year + "-";

	if (Month >= 10) {
		CurrentDate = CurrentDate + Month + "-";
	} else {
		CurrentDate = CurrentDate + "0" + Month + "-";
	}

	if (Day >= 10) {
		CurrentDate = CurrentDate + Day;
	} else {
		CurrentDate = CurrentDate + "0" + Day;
	}

	Hour = Hour >= 10 ? Hour : "0" + Hour;
	Min = Min >= 10 ? Min : "0" + Min;
	Sec = Sec >= 10 ? Sec : "0" + Sec;

	if (showSec == null || showSec) {
		CurrentDate = CurrentDate + " " + Hour + ":" + Min + ":" + Sec;
	} else {
		CurrentDate = CurrentDate + " " + Hour + ":" + Min;
	}

	return CurrentDate;
};

/**
 * 将日期对象转变为string
 * 
 * @param {}
 *            dateObj
 * @return 返回（时间）格式字符串，如16:36:00
 */
XMAppUtils.changeDateToTimeStr = function(dateObj) {
	var Hour = 0;
	var Min = 0;
	var Sec = 0;
	var CurrentTime = "";

	var date = new Date(dateObj["time"]);
	Hour = date.getHours();
	Min = date.getMinutes();
	Sec = date.getSeconds();

	Hour = Hour >= 10 ? Hour : "0" + Hour;
	Min = Min >= 10 ? Min : "0" + Min;
	Sec = Sec >= 10 ? Sec : "0" + Sec;
	CurrentTime = Hour + ":" + Min + ":" + Sec;

	return CurrentTime;
};

/**
 * 自动将图片按比列缩放
 * 
 * @return
 */
XMAppUtils.setImgSize1 = function(imgObj) {
	imgObj.width = 230;
	imgObj.height = 118;
};

XMAppUtils.setImgSize12 = function(imgObj) {
	imgObj.width = 170;
	imgObj.height = 110;
};

/**
 * 判断输入的价格必须符合：正整数|| 两位浮点数 || 0 
 * @param {} value
 * @return {Boolean}
 */
XMAppUtils.isRMB = function(value){
	if(/^0\.\d{1,2}$|^[1-9]\d*\.\d{1,2}$|^[1-9]\d*$|^0$/.test(value)){
		return true;
	}
	
	return false;
}
